package com.platform.service.shop.impl;

import com.platform.controller.shop.SendSms;
import com.platform.dao.shop.MlsUserDao;
import com.platform.dao.shop.UserDao;
import com.platform.dao.shop.UserRecordDao;
import com.platform.entity.MlsUserEntity2;
import com.platform.entity.shop.UserEntity;
import com.platform.entity.shop.UserRecordEntity;
import com.platform.entity.shop.UserRecordRefundCash;
import com.platform.service.shop.MlsUserService;
import com.platform.service.shop.UserRecordService;
import com.platform.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by zhouhaisheng on 2019/3/2.
 */
@Service
@Slf4j
public class UserRecordServiceImpl implements UserRecordService {

    @Autowired
    private UserRecordDao userRecordDao;

    @Autowired
    private MlsUserDao mlsUserDao;

    @Autowired
    private SendSms sendSms;

    @Autowired
    private UserDao userDao;

    @Override
    public List<UserRecordEntity> queryList(Map<String, Object> map) {
        return userRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userRecordDao.queryTotal(map);
    }

    @Override
    public void refundCash(UserRecordRefundCash userRecordRefundCash) {
        if(StringUtils.isNotEmpty(userRecordRefundCash.getReason())){
            // 更新驳回理由
            userRecordDao.refundCash(userRecordRefundCash);

            // 将钱退回给用户
            MlsUserEntity2 mlsUserVo = new MlsUserEntity2();
            mlsUserVo.setUserId(new Long(userRecordRefundCash.getUserId()));
            mlsUserVo.setGetProfit(userRecordRefundCash.getPrice());
            mlsUserVo.setGetActivityProfit(0);
            mlsUserVo.setGetPoints(0);
            mlsUserDao.updateMoney(mlsUserVo);

            // 发送邮件
            List<String> templateParams = new ArrayList<>();
            templateParams.add(userRecordRefundCash.getReason());

            UserEntity userEntity = userDao.queryObject(userRecordRefundCash.getUserId());
            String mobile = userEntity.getMobile();
            if(!StringUtils.isNotEmpty(mobile)){
                mobile = userRecordRefundCash.getMobile();
            }

            if(StringUtils.isNotEmpty(mobile)){
                sendSms.send(templateParams, SendSms.SEND_REFUND_CASH, Arrays.asList(new String[]{mobile}));
            }else{
                log.error("发送驳回短信失败，用户：{}未绑定手机号", userRecordRefundCash.getUserName());
            }

        }

    }
}
